package com.laicunba.event.listener;

import java.util.HashMap;
import java.util.Map;

import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.laicunba.event.JzhAccountCreateEvent;
import com.laicunba.pojo.JzhAccountDO;
import com.laicunba.pojo.TemplateDO;
import com.laicunba.service.MobileMessageService;
import com.laicunba.service.TemplateService;
import com.laicunba.service.UserService;
import com.laicunba.service.mq.ActiveMQMessageFactory;
import com.laicunba.service.mq.ActiveMQService;
import com.laicunba.util.PidUtil;

@Component
public class JzhAccountCreateEventListener implements ApplicationListener<JzhAccountCreateEvent> {

	@Autowired
	private ActiveMQService activeMQService;
	@Autowired
	private MobileMessageService mobileMessageService;
	@Autowired
	private TemplateService templateService;

	private static TemplateDO MSG_TEMPLATE;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void onApplicationEvent(JzhAccountCreateEvent event) {
		JzhAccountDO jzhAccountDO = (JzhAccountDO) event.getSource();
		if (jzhAccountDO == null) {
			return;
		}
		JSONObject msg = ActiveMQMessageFactory.createMessage("JZH_ACCOUNT_DO", jzhAccountDO);
		activeMQService.sendTopics("lcb.core.jzh.account.create.success", msg.toJSONString());
		// 发送开通短信
		if (MSG_TEMPLATE == null) {
			MSG_TEMPLATE = templateService.findByCodeWidthDefault("JZH_OPEN");
		}
		Map map = new HashMap();
		map.put("sex", "1".equals(PidUtil.findSex(jzhAccountDO.getCertifId())) ? "先生" : "女士");
		map.put("name", jzhAccountDO.getCustNm());
		String message = templateService.simpleMerge(MSG_TEMPLATE.getContent(), map);
		mobileMessageService.sendMsgAsync(jzhAccountDO.getMobileNo(), message);
	}

}
